package cn.bdqn.servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.bdqn.bean.User;
import cn.bdqn.service.UserService;
import cn.bdqn.service.impl.UserServiceImpl;

public class UserAddServlet extends HttpServlet {

	/**
		 * The doGet method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to get.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
		 * The doPost method of the servlet. <br>
		 *
		 * This method is called when a form has its tag value method equals to post.
		 * 
		 * @param request the request send by the client to the server
		 * @param response the response send by the server to the client
		 * @throws ServletException if an error occurred
		 * @throws IOException if an error occurred
		 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		request.setCharacterEncoding("UTF-8");
		UserService userService = new UserServiceImpl();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		//获取用户的输入
		String userCode = request.getParameter("userId");
		String password = request.getParameter("userpassword");
		String passRemi = request.getParameter("userRemi");
		String userName = request.getParameter("userName");
		String genderStr = request.getParameter("gender");
		String birth = request.getParameter("data");
		String phone = request.getParameter("userphone");
		String address = request.getParameter("userAddress");
		String typeStr = request.getParameter("userlei");
		
		//处理数据类型不一致的数据
		
		int gender = 0;
		if(genderStr !=null  && genderStr!=""){
			gender = Integer.parseInt(genderStr);
		}
		int typeNo = 0;
		if(typeStr !=null  && typeStr!=""){
			typeNo = Integer.parseInt(typeStr);
		}
		Date birthdate = null;
		if(birth !=null  && birth!=""){
			try {
				birthdate = sdf.parse(birth);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(passRemi.equals(password)){
			User user = new User(userCode, password, userName, 
					gender, birthdate, phone, address, typeNo);
			//判断用户是否存在
			User userExist = userService.login(user);
			if(userExist.getUserCode()==null){
				//添加创建人信息
				User u = (User) request.getSession().getAttribute("user");
				user.setCreateBy(u.getId());
				//调用修改方法
				if(userService.addUser(user)){
					response.sendRedirect("UserListServlet");
				}else{
					response.sendRedirect("jsp/userAdd.jsp");
				}
			}else{
				//用户已存在，不能注册
				response.sendRedirect("jsp/userAdd.jsp");
			}
		}else{
			response.sendRedirect("jsp/userAdd.jsp");
		}
	}

}
